package net.jxta.myjxta.presence;

public class PeerStatus {

    private final String stateID;
    private final String statusMessage;

    private PeerStatus(String p_state, String p_statusMessage) {
        stateID = p_state;
        if (p_statusMessage != null) {
            statusMessage = p_statusMessage;
        } else {
            statusMessage = "";
        }
    }

    public String getState() {
        return stateID;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public static PeerStatus getOnlineState(String message) {
        return new PeerStatus("online", message);
    }

    public static PeerStatus getAwayState(String message) {
        return new PeerStatus("away", message);
    }

    public static PeerStatus getUnknownState() {
        return new PeerStatus("unknown", null);
    }

    public static PeerStatus getOfflineState(String message) {
        return new PeerStatus("offline", message);
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final PeerStatus that = (PeerStatus) o;

        if (!stateID.equals(that.stateID)) return false;
        if (!statusMessage.equals(that.statusMessage)) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = stateID.hashCode();
        result = 29 * result + statusMessage.hashCode();
        return result;
    }

    public boolean isOffline() {
        String offlineState = PeerStatus.getOfflineState("").getState();
        return getState().equals(offlineState);
    }

    public boolean isAway() {
        String awayState = PeerStatus.getAwayState("").getState();
        return getState().equals(awayState);
    }

    public static PeerStatus getPeerStatus(String senderStateFromTheNetwork, String userDefinedMessage) {
        if ("online".equalsIgnoreCase(senderStateFromTheNetwork)) {
            return getOnlineState(userDefinedMessage);
        } else if ("offline".equalsIgnoreCase(senderStateFromTheNetwork)) {
            return getOfflineState(userDefinedMessage);
        } else if ("away".equalsIgnoreCase(senderStateFromTheNetwork)) {
            return getAwayState(userDefinedMessage);
        } else
            return getUnknownState();

    }

}
